// Assignment #: 8
// This file is given by the instructor.
// Description: This is a utility class that takes a string containing
// a customer's information and parse it to create a customer object.

import java.util.StringTokenizer;

public class CustomerParser {

 public static Customer customerParser(String lineToParse)
  {
   String temp1 = new String();
   String temp2 = new String();
   String temp3 = new String();
   String temp4 = new String();
   String name = new String();

   Customer customer = new Customer();

   String[] tokens = lineToParse.split(":");

     // extract the name of the customer
     if (tokens.length > 0 && tokens[0].length() > 0)
      {
       name = tokens[0].trim();

       String[] tokens2 = name.split(",");

       // extract the last name of the customer
       if (tokens2.length > 0 && tokens2[0].length() > 0)
        {
         temp1 = tokens2[0].trim();
         customer.setLastName(temp1.trim());
        }

       // extract the first name of the customer
       if (tokens2.length > 0 && tokens2[1].length() > 0)
        {
         temp2 = tokens2[1].trim();
         customer.setFirstName(temp2.trim());
        }
      }

     // extract the customer ID
     if (tokens.length > 1 && tokens[1].length() > 0)
      {
       temp3 = tokens[1].trim();
       customer.setCustomerID(temp3.trim());
      }

     try
      {
       // extract the cash amount that the customer has
       if (tokens.length > 2 && tokens[2].length() > 0)
        {
         temp4 = tokens[2].trim();
         customer.setCashAmount( Double.parseDouble(temp4.trim()));
        }
      }
     catch(NumberFormatException exception)
      {
       System.out.print("Invalid String\n");
      }

     return customer;
   }
} // end of CustomerParser class


